package com.cy.login.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.util.JwtUtils;
import com.cy.login.comment.exection.ServiceException;
import com.cy.login.mapper.AccountMapper;
import com.cy.login.domain.Account;
import com.cy.login.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.Optional;

/**
 * 账号表(Account)表服务实现类
 *
 * @author cxy
 * @since 2024-07-10 15:04:36
 */
@Slf4j
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private ValueOperations<Object, Object> valueOperations;

    @Transient
    @Override
    public Account loginEmail(Account account) {
        if (StringUtils.isEmpty(account.getEmail()) || StringUtils.isEmpty(account.getPassword())) {
            throw new ServiceException("邮箱和密码不能为空");
        }
        Account po = (Account) valueOperations.get(account.getEmail());  // 查缓
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        if (po == null) { // 缓为空
            wrapper.eq(Account::getEmail, account.getEmail());
            po = accountMapper.selectOne(wrapper);
            if (po == null) {
                po = new Account();
                po.setEmail(account.getEmail());
                po.setPassword(MD5.create().digestHex(account.getPassword()));
                po.setCreateTime(new Date());
                po.setLastTime(new Date());
                accountMapper.insert(po);
                // 创建token
                String jwtToken = JwtUtils.createJwtToken(po.getEmail());
                po.setToken(jwtToken);
                valueOperations.set(po.getEmail(), po);
            }
        }
        // 缓存非空
        Optional<Integer> isLock = Optional.ofNullable(po.getIsLock());
        if (isLock.isPresent() && isLock.get() == 1) {
            throw new ServiceException("账号被禁用,截至时间"+po.getLockTime());
        }
        // 更新登录时间
        wrapper.eq(Account::getEmail, account.getEmail());
        po.setLastTime(new Date());
        accountMapper.update(po,wrapper);
        po = accountMapper.selectOne(wrapper);
        String jwtToken = JwtUtils.createJwtToken(po.getEmail());
        po.setToken(jwtToken);
        valueOperations.set(po.getEmail(), po);
        if (!MD5.create().digestHex(account.getPassword()).equals(po.getPassword())) { // 密码错误
            throw new ServiceException("密码错误");
        }
        // 隐藏密码
        po.setPassword(null);
        return po;
    }

    @Override
    public Void logout(String userName) {
        if (Boolean.FALSE.equals(valueOperations.getOperations().delete(userName))) {
            throw new ServiceException("不存在登录信息");
        }
        return null;
    }
}
